﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.TemplateLanguage.Properties;

namespace MethodWorx.CMS.TemplateLanguage.Parsers.Html
{
    public class ValidationMessage : IDesignerInitialization
    {
        [Property(Alias = "Name")]
        public string ControlName { get; set; }

        [Property(Alias = "Class")]
        public string CssClass { get; set; }

        public ValidationMessage()
        {
            this.CssClass = "error";
        }

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            //  check
            if (!string.IsNullOrEmpty(this.ControlName))
            {
                var errors = context.WorkflowExecutionContext.GetWorkflowErrors(this.ControlName);
                if (errors != null && errors.Any())
                    return "<span class=\"" + this.CssClass + "\">" + errors.First() + "</span>";
            }

            return null;
        }

        public string Name
        {
            get { return "validation-message"; }
        }

        public string Namespace
        {
            get { return "html"; }
        }

        public string[] Parameters
        {
            get { return new string[] { "name" }; }
        }

        public void Design(IDesigner designer)
        {
            designer.AddControl("Html:Validation-Message")
                .SetControlType(DesignerControlType.Label)
                .AddContentBeforeChildren("<span class=\"" + this.CssClass + "\"><em>Validation Message For: '" + this.ControlName + "'</em></span>");
        }

        public string AddToTemplate(Xml.IXmlElement input, string initializer)
        {
            return ControlInitializerHelper.AddToTemplate("<html:validation-message />", input, initializer );
        }

        public string[] GetInitializers()
        {
            return new string[] {"Insert Before", "Insert After", "Insert Child"};
        }
    }
}
